/**
 * @author Manoj
 * This class is used for obtaining html objects and its properties from a text file.
 * The text file is file in a specific format containing objects and its properties.  
 */

/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guifilegenerator;

import java.io.RandomAccessFile;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;

import utility.Input;

public class FileObjectsfinder
{
    private RandomAccessFile raf;  
    private Hashtable<Object,LinkedList<Object>> object_Properties=new Hashtable<Object,LinkedList<Object>> (); // to save all properties
    
   /**
     * The constructor accepts filename as string and a char which separates objects 
     * from its properties.  
     * @param FileName It is the filename from which tag objects are extracted.     * 
     */
    public FileObjectsfinder(String FileName)
    {
           //this.fileName= FileName;
           try 
           {
               this.raf= new RandomAccessFile(FileName,"r");
           } 
           catch(Exception e) 
           {
        	   e.printStackTrace(); 
           }
                       
    }
   
    /**
     * This method returns a Hashtable<Object,LinkedList<Object>> which contains 
     * html tags objects as keys and its enclosing tags or data as a list of type
     * LinkedList<Object>.  
     * @return Hashtable containing objects and itsa properties.
     */
    public  Hashtable<Object,LinkedList<Object>> getobject_Properties()   // Object currentLevel,int fromLine, int toLine)
    {
             LinkedList<Object> listl= new LinkedList<Object>();                    
             String cuurentObject= null;
             try
             {
	              LinkedList<String> SequenceKeeper = new LinkedList<String>();                                      
	              String MaserRecord[] = raf.readLine().split(Input.SEPARATOR);
	              cuurentObject = MaserRecord[1];
	              SequenceKeeper.add(cuurentObject);
	              object_Properties.put(cuurentObject,listl);
	              
	              int lastLevel = 0;               
	              String line = null;                                 
	              int LineNum = 0;            
	              
	              while((line=raf.readLine())!= null)
	              {            
	            	  char[] chararry = line.toCharArray();
	            	  int Level = 1;
	                
	            	  while(String.valueOf(chararry[Level]).equalsIgnoreCase(Input.SEPARATOR))
	            	  {
	                    Level++;
	            	  }
	                   
	                   LineNum++;
	                   line = line.substring(Level);
	                   
	                   String  tempSplit[] = line.split(Input.SEPARATOR);
	                   
	                   if(lastLevel == Level)
	                   {
		                    lastLevel = Level;
		                    listl.add(line);                           
	                   }
	                   else if(lastLevel < Level)
	                   {
	                       LinkedList<Object> tempList= new LinkedList<Object>();
	                       tempList.addAll(listl);
	                       object_Properties.put(cuurentObject,tempList);
	                       
	                       if(!listl.isEmpty())
		                   {                                                                                                                         
		
		                       String temp[]=listl.get(listl.size()-1).toString().split(Input.SEPARATOR);
		                       cuurentObject=temp[1];
		                       if(!SequenceKeeper.contains(cuurentObject))
		                            SequenceKeeper.add(cuurentObject);
		                   }
	                       	listl.clear();
	                       	listl.add(line);
	                           lastLevel=Level;
	                   }	                                  
	                   else if(lastLevel>Level)
	                   {
		                 int Difference=lastLevel-Level;
		                 lastLevel=Level+1;
		                 LinkedList<Object> tempListG= new LinkedList<Object>();
		                 tempListG.addAll(listl);
		                 object_Properties.put(cuurentObject,tempListG);
		                                                   
		                 listl.clear();
		              	               
		                 int SIZE= SequenceKeeper.size();
		                 Iterator it =SequenceKeeper.iterator();
		                 LinkedList<String>tempSubLinkedList=new LinkedList<String>();
		                 
		                 for(int i=0;i<SIZE-Difference;i++)
	                     { 
		                      tempSubLinkedList.addLast(it.next().toString());
	                     }
	                                             
	                      SequenceKeeper=tempSubLinkedList;
	                      cuurentObject=SequenceKeeper.get(SequenceKeeper.size()-1);
	                      object_Properties.get(cuurentObject).add(line); 
	                     
	                      cuurentObject=tempSplit[1];
	                      SequenceKeeper.addLast(cuurentObject);                                                                                          
	                   }                                                                   
	              } 
	              	             
	               LinkedList<Object> tempList= new LinkedList<Object>();
	               tempList.addAll(listl);
	               object_Properties.put(cuurentObject,tempList);
	  
	               //This Displays the Hash MAP  Containts                                                                                                                                 
	         		Iterator it = object_Properties.keySet().iterator();                             
	           		while(it.hasNext())
	                      {
	                       String  Key=it.next().toString();
	                        System.out.println("KEY IS:"+Key);
	                        System.out.println("ELEMENTS ARE:"+object_Properties.get(Key));
	                        
	                      } 
	           		System.out.println("===========================================================================================================");
	  
                }
                catch(Exception e)
                {
                	e.printStackTrace();
	                LinkedList<Object> tempList= new LinkedList<Object>();
	                tempList.addAll(listl);
	                object_Properties.put(cuurentObject,tempList);
	                //e.printStackTrace();
                }
        return object_Properties;
    }
    
    /**
     * This method is same as getobject_Properties() method except it returns a LinkedHashMap<Object,LinkedList<Object>>
     * instead of Hashtable.
     * @return Hashmap contaning objects as keys and its properties as values.
     */
    public  LinkedHashMap<Object,LinkedList<Object>> getObjectProperties()   // Object currentLevel,int fromLine, int toLine)
    {
             LinkedList<Object> listl= new LinkedList<Object>();                    
             String cuurentObject= null;
             LinkedHashMap<Object, LinkedList<Object>> Objectproperties = new LinkedHashMap<Object, LinkedList<Object>>();
             try
             {
	              LinkedList<String> SequenceKeeper = new LinkedList<String>();                                      
	              String MaserRecord[] = raf.readLine().split(Input.SEPARATOR);
	              cuurentObject = MaserRecord[1];
	              SequenceKeeper.add(cuurentObject);
	              Objectproperties.put(cuurentObject,listl);
	              
	              int lastLevel = 0;               
	              String line = null;                                 
	              int LineNum = 0;            
	              
	              while((line=raf.readLine())!= null)
	              {            
	            	  char[] chararry = line.toCharArray();
	            	  int Level = 1;
	                
	            	  while(String.valueOf(chararry[Level]).equalsIgnoreCase(Input.SEPARATOR))
	            	  {
	                    Level++;
	            	  }
	                   
	                   LineNum++;
	                   line = line.substring(Level);
	                   
	                   String  tempSplit[] = line.split(Input.SEPARATOR);
	                   
	                   if(lastLevel == Level)
	                   {
		                    lastLevel = Level;
		                    listl.add(line);                           
	                   }
	                   else if(lastLevel < Level)
	                   {
	                       LinkedList<Object> tempList= new LinkedList<Object>();
	                       tempList.addAll(listl);
	                       Objectproperties.put(cuurentObject,tempList);
	                       
	                       if(!listl.isEmpty())
		                   {                                                                                                                         
		
		                       String temp[]=listl.get(listl.size()-1).toString().split(Input.SEPARATOR);
		                       cuurentObject=temp[1];
		                       if(!SequenceKeeper.contains(cuurentObject))
		                            SequenceKeeper.add(cuurentObject);
		                   }
	                       	listl.clear();
	                       	listl.add(line);
	                           lastLevel=Level;
	                   }	                                  
	                   else if(lastLevel>Level)
	                   {
		                 int Difference=lastLevel-Level;
		                 lastLevel=Level+1;
		                 LinkedList<Object> tempListG= new LinkedList<Object>();
		                 tempListG.addAll(listl);
		                 Objectproperties.put(cuurentObject,tempListG);
		                                                   
		                 listl.clear();
		              	               
		                 int SIZE= SequenceKeeper.size();
		                 Iterator it =SequenceKeeper.iterator();
		                 LinkedList<String>tempSubLinkedList=new LinkedList<String>();
		                 
		                 for(int i=0;i<SIZE-Difference;i++)
	                     { 
		                      tempSubLinkedList.addLast(it.next().toString());
	                     }
	                                             
	                      SequenceKeeper=tempSubLinkedList;
	                      cuurentObject=SequenceKeeper.get(SequenceKeeper.size()-1);
	                      Objectproperties.get(cuurentObject).add(line); 
	                     
	                      cuurentObject=tempSplit[1];
	                      SequenceKeeper.addLast(cuurentObject);                                                                                          
	                   }                                                                   
	              } 
	              	             
	               LinkedList<Object> tempList= new LinkedList<Object>();
	               tempList.addAll(listl);
	               Objectproperties.put(cuurentObject,tempList);
	  
	               //This Displays the Hash MAP  Containts                                                                                                                                 
	         		Iterator it = Objectproperties.keySet().iterator();                             
	           		while(it.hasNext())
	                      {
	                       String  Key=it.next().toString();
	                        System.out.println("KEY IS:"+Key);
	                        System.out.println("ELEMENTS ARE:"+Objectproperties.get(Key));
	                        
	                      } 
	           		System.out.println("===========================================================================================================");
	  
                }
                catch(Exception e)
                {
                                                                          
	                LinkedList<Object> tempList= new LinkedList<Object>();
	                tempList.addAll(listl);
	                Objectproperties.put(cuurentObject,tempList);
	                //e.printStackTrace();
                }
        return Objectproperties;
    }
    
    /*public static void main(String [] arg)
    {
    	FileObjectsfinder find = new FileObjectsfinder("//home//wiki22//Reports//WebReports//WebReportsTextFiles//Employee.txt");
    	Hashtable <Object,LinkedList<Object>> hash = find.getobject_Properties();
    	System.out.println("The hash is "+hash);
    }*/
    
    
}
    

